package cn.com.tianpeidong.restaurantadmin.project.admin.user.rest;

import cn.com.tianpeidong.restaurantadmin.common.exception.BusinessException;
import cn.com.tianpeidong.restaurantadmin.common.util.msg.MessageUtils;
import cn.com.tianpeidong.restaurantadmin.common.util.page.PageWrapper;
import cn.com.tianpeidong.restaurantadmin.common.util.security.ShiroUtils;
import cn.com.tianpeidong.restaurantadmin.project.admin.user.entity.UserInfo;
import cn.com.tianpeidong.restaurantadmin.project.admin.user.service.IUserInfoService;
import cn.com.tianpeidong.restaurantadmin.project.admin.user.vo.ModifyPasswd;
import cn.com.tianpeidong.restaurantadmin.project.admin.user.vo.UserVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;

/**
 * @author tianpeidong
 * @version 创建时间: 19-4-7 下午5:22
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserInfoService userInfoService;

    @GetMapping
    @RequiresPermissions("user:list")
    public ResponseEntity list(
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "page", required = false) Integer pageNum) {
        PageWrapper pageWrapper = new PageWrapper(pageSize, pageNum);
        List<UserVO> list = userInfoService.listUser(new UserVO());
        return ResponseEntity.ok(pageWrapper.getResult(list));
    }

    @PostMapping("/search")
    @RequiresPermissions("user:list")
    public ResponseEntity search(
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestBody UserVO userVO) {
        PageWrapper pageWrapper = new PageWrapper(pageSize, pageNum);
        List<UserVO> list = userInfoService.listUser(userVO);
        return ResponseEntity.ok(pageWrapper.getResult(list));
    }

    @GetMapping("/{id}")
    @RequiresPermissions("user:list")
    public ResponseEntity getDishesById(@PathVariable("id") Long id) {
        UserVO userVO = userInfoService.getUserInfoById(id);
        return ResponseEntity.ok(userVO);
    }

    @PostMapping
    @RequiresPermissions("user:add")
    public ResponseEntity add(@Valid @RequestBody UserVO userVO) {
        UserInfo userInfo = userInfoService.add(userVO);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(userInfo.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @PutMapping
    @RequiresPermissions("user:edit")
    public ResponseEntity edit(@Valid @RequestBody UserVO userVO) {
        UserInfo userInfo = userInfoService.edit(userVO);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(userInfo.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @PatchMapping("/{userId}")
    @RequiresPermissions("user:edit")
    public ResponseEntity resetPasswd(@PathVariable("userId") Long userId) {
        UserInfo userInfo = userInfoService.resetPasswd(userId);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(userInfo.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @DeleteMapping("/{userId}")
    @RequiresPermissions("user:delete")
    public ResponseEntity delete(@PathVariable("userId") Long userId) {
        userInfoService.delete(userId);
        return ResponseEntity.noContent().build();
    }

    @PatchMapping("/{userId}/modifyPasswd")
    public ResponseEntity modifyPasswd(
            @PathVariable("userId") Long userId,
            @RequestBody ModifyPasswd modifyPasswd) {
        Long curUserId = ShiroUtils.getUserId();
        if (!userId.equals(curUserId)) {
            throw new BusinessException(MessageUtils.message("user.not.match"));
        }
        UserInfo userInfo = userInfoService.modifyPasswd(modifyPasswd);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(userInfo.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }


}
